<template>
  <div class="user-container">
    <!-- 用户信息 -->
    <div class="info-box">
      <van-image
        round
        fit="cover"
        width="60px"
        height="60px"
        :src="userinfo.photo"
      />
      <h2>
        {{ userinfo.name }}
        <br />
        <van-tag color="#fff" text-color="#1989FA">{{
          userinfo.birthday
        }}</van-tag>
      </h2>
    </div>
    <!-- 操作导航栏 -->
    <van-row type="flex">
      <van-col span="8">
        <van-icon name="label-o" color="#abc4fe" />
        <span>我的作品</span>
      </van-col>
      <van-col span="8">
        <van-icon name="star-o" color="#ff4e94" />
        <span>我的收藏</span>
      </van-col>
      <van-col span="8">
        <van-icon name="clock-o" color="#ffc782" />
        <span>阅读记录</span>
      </van-col>
    </van-row>
    <!-- cell列表 -->
    <van-cell-group>
      <van-cell title="编辑资料" icon="edit" is-link @click="$router.push({path:'/edit'})" />
      <van-cell title="小智同学" icon="comment-o" is-link @click="$router.push({path:'/student'})"/>
      <van-cell title="系统设置" icon="setting-o" is-link />
      <van-cell title="退出登录" icon="warning-o" is-link @click="logout" />
    </van-cell-group>
  </div>
</template>

<script>
import { getuserinfo } from './user'
import { removeToken } from '@/utils/token.js'
export default {
  name: 'user',
  data () {
    return {
      userinfo: {}
    }
  },
  methods: {
    logout () {
      this.$dialog
        .confirm({
          title: '提示',
          message: '是否要退出登录'
        })
        .then(() => {
          // on confirm
          // 清除token
          removeToken()
          // 清除本地信息
          this.userinfo = {}
          // 跳转登录页
          this.$router.push({path: '/login'})
        })
        .catch(() => {
          // on cancel
        })
    }
  },
  created () {
    getuserinfo().then(res => {
      // console.log(res);
      this.userinfo = res.data.data
      this.$store.commit('updateUserInfo',res.data.data)
    })
  }
}
</script>

<style lang="less">
.user-container {
  // 用户个人信息
  .info-box {
    height: 100px;
    background-color: #1989fa;
    display: flex;
    align-items: center;
    h2 {
      font-size: 18px;
      color: #fff;
      font-weight: 400;
    }
    .van-image {
      margin: 0 15px;
    }
  }
  // 导航栏
  .van-row {
    .van-col {
      text-align: center;
      span {
        display: block;
        font-size: 12px;
        font-weight: 500;
      }
      .van-icon {
        padding: 10px 0;
        font-size: 30px;
      }
    }
  }
  // cell列表
  .van-cell {
    &:first-child {
      margin-top: 15px;
    }
  }
}
</style>
